package com.sskj.cndragon.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.ui.activity.AddressManagerActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.List;

import io.reactivex.Flowable;

public class AddressManagerActivityPresenter extends BasePresenter<AddressManagerActivity> {

    public Flowable<List<AddressBean>> getAddresses(String userID) {
        return httpService.getAddresses(userID)
                .tag(this)
                .converter(new JsonConverter<HttpData<List<AddressBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData());
    }

    public void setAddressDefault(String userId, String addressId) {
        httpService.setAddressDefault(userId, addressId)
                .tag(this)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData body = response.body();
                        ToastUtil.showShort(body.getMsg());
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.setDefaultSuccess();
                        }
                    }
                });
    }

    public void delAddress(String addressId) {
        httpService.delAddress(addressId)
        .tag(this)
        .execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData body = response.body();
                ToastUtil.showShort(body.getMsg());
                if (body.getStatus() == HttpConfig.OK) {
                    mView.delSuccess();
                }
            }
        });
    }
}
